<!-- 历史数据处理 -->
<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目类型" prop="pmoprojecttype" :rules="rules.validateRequire">
            <el-select v-model="model.pmoprojecttype" @change="dealTypeChange">
              <el-option v-for="item in codeList.pmoprojecttypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="ifShowReview">
          <el-form-item label="项目结果" prop="projectresult" :rules="rules.validateRequire">
            <el-select v-model="model.projectresult" @change="dealResultChange">
              <el-option v-for="item in projectResultList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="ifShowReview">
          <el-form-item label="结果公布日期" prop="resulttime">
            <el-date-picker v-model="model.resulttime" type="date" placeholder="例：2021-01-01" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doSubmitEdit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'

/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: {},
  data() {
    return {
      moduleTitle: '历史数据',
      urlPrefix: '/mpm/project/',
      url: {
        queryReviewRecord: '/mpm/projectReview/list',
        savePmoProjectType: '/mpm/projectReview/savePmoProjectType'
      },
      projectData: {},
      ifShowReview: false
    }
  },
  props: {
    codeList: { type: Object, default: () => null }
  },
  computed: {
    projectResultList() {
      let result = []
      if (!this.model.pmoprojecttype) {
        return result
      } else if (this.model.pmoprojecttype === '0') {
        // 推广类处理
        result = this.codeList.projectresultList.slice(8, 11)
        return result
      } else {
        // 销售类处理
          result = this.codeList.projectresultList.slice(0, 8)
          return result
      }
    }
  },
  methods: {
    dealResultChange() {},
    dealTypeChange() {
      this.$set(this.model, 'projectresult', '')
    },
    open() {
      this.model = {}
      this.ifShowReview = false
      // 查询是否有复盘单据，决定项目结果是否显示
      let queryParam = { sidx: 'sortcode', sord: 'asc', currentPage: 1, pageSize: 10 }
      queryParam.qry_projectcode = this.projectData.projectcode
      queryParam.qry_usercode = 1
      getAction(this.url.queryReviewRecord, queryParam).then((res) => {
        if (res.success) {
          this.ifShowReview = res.result.dataList.length > 0 ? true : null
        } else {
          this.$tap.alert('查询复盘记录失败，请联系管理员', 'warning')
        }
        this.saveLoading = false
      })
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        this.title = '处理' + this.moduleTitle
      })
      this.$set(this.model, 'pmoprojecttype', this.projectData.pmoprojecttype)
      this.$set(this.model, 'projectresult', this.projectData.projectresult)
    },
    doSubmitEdit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.saveLoading = true
          let params = {}
          params.pmoprojecttype = this.model.pmoprojecttype
          params.projectcode = this.projectData.projectcode
          params.result = this.model.projectresult
          params.resulttime = this.model.resulttime
          putAction(this.url.savePmoProjectType, params).then((res) => {
            if (res.success) {
              this.$tap.message('修改成功', 'success')
              this.visible = false
              this.$emit('refresh')
            } else {
              this.$tap.alert('修改失败，请联系管理员', 'warning')
            }
            this.saveLoading = false
          })
        } else {
          this.$tap.alert('请填写必要内容', 'warning')
        }
      })
    }
  }
}
</script>
